package com.deepocean.exception;


import com.deepocean.common.entity.ResultEntity;
import com.deepocean.utils.MessageSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class CoalWashingPlantExceptionHandler {
    @Autowired
    MessageSourceUtil messageSourceUtil;

    @ExceptionHandler(value = Exception.class)
    public ResultEntity exceptionHandler(Exception exception) {
        Map<String, String> failMap = new HashMap();
        exception.printStackTrace();
        if (exception instanceof CoalWashingPlantException) {
            CoalWashingPlantException coalWashingPlantException = (CoalWashingPlantException) exception;
            String errorMessage = messageSourceUtil.getMessage(coalWashingPlantException.getErrorCode());
            coalWashingPlantException.setErrorMessage(errorMessage);
            failMap.put("errorCode", coalWashingPlantException.getErrorCode());
            failMap.put("errorMessage", coalWashingPlantException.getErrorMessage());
            return ResultEntity.failed(failMap);
        } else {
            failMap.put("errorCode", "10001");
            failMap.put("errorMessage", "未知错误");
            return ResultEntity.failed(failMap);
        }
    }
}
